unit dbcClasses;
// Database Comparer VCL v.3.2
// Copyright (C) 1998-2012, by Boris Loboda
// www.CleverComponents.com
{$I Comparer.INC}

// History:
// 05.06.2004 (BL)
// - property IBServerOptions added in TCustomDBComparer (TDBComparer).
// 28.11.2007 (BL)
// - properties: IncludeObjsMask, ExcludeObjsMask
// 21.07.2008 (BL)
// - Views recreate fix (issue by Kadri Izer).

interface
{ TODO -oBarry : Save (load) database structure to stream. }

Uses SysUtils,
     Classes,
     {$IFDEF Delphi6} Variants,{$ENDIF}
     DB,
     Contnrs,
     dbcTypes,
     dbcDBOptions,
     dbcMsgMngr;

const dbcVersion = '6.2';

type
  TCustomDBComparer    = Class;
  TCustomDBStructure   = Class;
  TCustomSQLExec       = Class;
  TStatement           = Class;
  TDbObj               = Class;

  // event types
  TStatementExecuteEvent = procedure(Sender: TObject; index: Integer) of Object;
  TStatementCreateEvent = procedure(Sender: TObject; var Statement: TStatement) of Object;
  TStatementAddEvent = procedure(st: TStatement; var Accept: Boolean) of Object;
  TGetParamValueEvent = procedure(Sender: TObject; Param: TParam) of Object;
  TAcceptObjEvent = procedure(Sender: Tobject; aObjName: String; aObjType: TDbObject; var Accept: Boolean) of Object;
  TAccept_ObjEvent = procedure(Sender: Tobject; aObj: TDbObj; var Accept: Boolean) of Object;

  // Summary database info
  TCustomObjs = class
  protected
    fDBStructure: TCustomDBStructure;
    fLinkedObjs: TCustomObjs;
    fTermString: String;
    fCompareObjects: TCmpObjSetType;
    fConnectionOptions: TConnectionOptions;
    fIBServerOptions: TIBServerOptions;
    fMSSQLServerOptions: TMSSQLServerOptions;
    fMySQLServerOptions: TMySQLServerOptions;
    fASAServerOptions: TASAServerOptions;
    fASEServerOptions: TASEServerOptions;
    fOracleServerOptions: TOracleServerOptions;
    fPGSQLServerOptions: TPGSQLServerOptions;
    fDB2ServerOptions: TDB2ServerOptions;
    fDatabaseType: TDatabaseType;
    fCompareRole: TCompareRoleType;
    fSQLExec: TCustomSQLExec;
    fOnLogNextLine: TLogNextLineEvent;
    fIdentQuote: TIdentQuoteType;
    fOnAccept_Obj: TAccept_ObjEvent;
    function GetObjsMaster(): TCustomObjs;
    function GetObjsTarget(): TCustomObjs;
    procedure SetCompareRole(aRole: TCompareRoleType);
    procedure InternalLinkDepend(d1Obj, d2Obj: TDbObj;
                                 FldName: String;
                                 aPosX: Integer;
                                 aPosY: Integer);
    // Remove Obj1 from Obj
    procedure ClearDepend(Obj: TDbObj; Obj1: TDbObj);
    procedure ErrMsg(aMsg: String);
    procedure SetDatabaseType(aType: TDatabaseType);
    function Accept_Obj(aObj: TDbObj): Boolean;
    function GetDbPrefix(): String;
  public
    // Objects list
    Sch: TStringList; // SCHEMA
    Dmn: TStringList; // DOMAIN
    Udf: TStringList; // UDF - EXTERNAL FUNCTION
    Trg: TStringList; // TRIGGER
    Prc: TStringList; // PROCEDURE
    Tbl: TStringList; // TABLE
    Exc: TStringList; // EXCEPTION
    Gnr: TStringList; // GENERATOR
    Idx: TStringList; // INDEX
    Viw: TStringList; // VIEW
    Rol: TStringList; // ROLE
    Gnt: TStringList; // CRANT
    Ctr: TStringList; // All CONSTRAINTS
    Prk: TStringList; // Primary keys
    Frk: TStringList; // Foreign keys
    Unq: TStringList; // Unique's
    Chk: TStringList; // Checks
    Ref: TStringList; // References constraints
    Cdf: TStringList; // CONSTRAINT DEFAULT (MS SQL)
    Rul: TStringList; // Rules (MS SQL)
    Def: TStringList; // Defaults (MS SQL)
    Pkg: TStringList; // Packages (Oracle)
    Pbl: TStringList; // Publications (Sybase)
    Opr: TStringList; // Operators (PostgreSQL)
    Opc: TStringList; // Oparator classes (PostgreSQL)
    Cnv: TStringList; // Conversion (PostgreSQL)
    Evt: TStringList; // Event (Sybase ASA) 
    // use for ExtractMetadata()
    MetaData: TStringList;
    CurrentSchema: String;
    CurrentSchemaPath: String;
    DBDescription: String;
    // extract from: script / database
    ExtractFrom: TExtractFromType;
    property ConnectionOptions: TConnectionOptions read fConnectionOptions;
    property IBServerOptions: TIBServerOptions read fIBServerOptions;
    property MSSQLServerOptions: TMSSQLServerOptions read fMSSQLServerOptions;
    property MySQLServerOptions: TMySQLServerOptions read fMySQLServerOptions;
    property ASAServerOptions: TASAServerOptions read fASAServerOptions;
    property ASEServerOptions: TASEServerOptions read fASEServerOptions;
    property OracleServerOptions: TOracleServerOptions read fOracleServerOptions;
    property PGSQLServerOptions: TPGSQLServerOptions read fPGSQLServerOptions;
    property DB2ServerOptions: TDB2ServerOptions read fDB2ServerOptions;
    property DatabaseType: TDatabaseType read fDatabaseType write SetDatabaseType;
    // master / target
    property CompareRole: TCompareRoleType read fCompareRole write SetCompareRole;
    property TermString: String read fTermString write fTermString;
    property DBStructure: TCustomDBStructure read fDBStructure write fDBStructure;
    property CompareObjects: TCmpObjSetType read fCompareObjects write fCompareObjects;
    property SQLExec: TCustomSQLExec read fSQLExec write fSQLExec;
    property ObjsMaster: TCustomObjs read GetObjsMaster;
    property ObjsTarget: TCustomObjs read GetObjsTarget;
    property db_prefix: String read GetDbPrefix;
    // for dbCustomOLEDB
    property IdentQuote: TIdentQuoteType read fIdentQuote write fIdentQuote;
    property OnLogNextLine: TLogNextLineEvent read fOnLogNextLine
                                              write fOnLogNextLine;
    property OnAccept_Obj: TAccept_ObjEvent read fOnAccept_Obj write fOnAccept_Obj;
    constructor Create;
    destructor Destroy; override;
    function ODSMajorVersionOfTarget(): Integer;
    // clear all extracted objects - empty database structure
    procedure Clear; virtual;
    procedure SetSourceDefinition(); virtual;
    // Make database connect-string
    function DbConnectStr(): String;
    procedure ExtractMetadata(); overload; virtual;
    procedure ExtractMetadata(const Script: TStrings); overload; virtual;
    function AddGrant(gntData: TDbObj): TDbObj;
    procedure SortGrants();
    procedure LinkDepend(d1Obj, d2Obj: TDbObj;
                         FldName: String;
                         aPosX: Integer;
                         aPosY: Integer);
    procedure DoLogNextLine(aText: String);
    // Remove all dependencies for aObj into Objs
    procedure RemoveDepend(aObj: TDBObj);
    procedure AddObj(aObj: TDbObj; FreeNotAdded: Boolean = True);
  end;

  TStatement = class
  protected
    fSQLExec: TCustomSQLExec;
    function GetCaption(): String;
  public
    // statement text
    Text: TStringList;
    Typ: TStatType;
    // start line into script
    Line: Integer;
    // statement text length
    Len: Integer;
    // disable for execute
    Disabled: Boolean;
    ObjType: TDBObject;
    Operation: TDbOperation;
    ObjName: String;
    OwnName: String; // table/view/procedure for Column-object
    Shema: String;
    Description: String;
    // for interactive selection
    Select: Boolean;
    // after execution - if error statement
    ErrMsg: String;
    Term: String;
    constructor Create(aSQLExec: TCustomSQLExec);
    Destructor Destroy; override;
    property Caption: String read GetCaption;
    property SQLExec: TCustomSQLExec read fSQLExec;
  end;

  // Result script storage
  TCustomSQLExec = Class(TComponent)
  private
    fCurrentSchema: String;
  protected
    // statements sequence
    fScriptFileName: TFileName;
    StatSeq: TObjectList;
    Line: Integer;
    // StatSeq have really update database statements
    fIsUpdate: Boolean;
    fBreakOnError: Boolean;
    fOnLogNextLine: TLogNextLineEvent;
    fOnProgressUpdate: TProgressUpdateEvent;
    fOnErrorMessage: TErrorMessageEvent;
    fOnBeforeStatementExecute: TStatementExecuteEvent;
    fOnAfterStatementExecute: TStatementExecuteEvent;
    fOnStatementCreate: TStatementCreateEvent;
    fOnStatementAdd: TStatementAddEvent;
    fAutoDDL: Boolean;
    fErrors: Integer;
    fDatabaseType: TDatabaseType;
    fShowFullLog: Boolean;
    fMsgMngr: TMsgManager;
    fOnLoadScript: TLoadScriptEvent;
    {$ifdef Delphi2009}
    fEncode: TdbcEncode;
    {$endif}
    procedure DoLogNextLine(LogLine: String); virtual;
    procedure DoProgressUpdate(Value, MaxValue: Integer); virtual;
    procedure DoErrorMessage(ErrMsg: String); virtual;
    function GetShowFullLog(): Boolean; virtual;
    procedure DoStatementAdd(statement: TStatement; var Accept: Boolean); virtual;
    procedure DoBeforeStatementExecute(index: Integer); virtual;
    procedure DoAfterStatementExecute(index: Integer); virtual;
    function GetStatement(ix: Integer): TStatement;
    procedure SetStatement(ix: Integer; aStatement: TStatement);
    function GetStatementsCount(): Integer;
    function DoStatementCreate(): TStatement; virtual;
    procedure SetCurrentSchema(aValue: string); virtual;
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    procedure ConnectToDatabase(); virtual;
    procedure DisconnectFromDatabase(); virtual;
    procedure ExecuteIxStatement(index: integer); virtual;
    procedure ExecuteStatement(st: TStatement); virtual;
    function IndexOf(st: TStatement): Integer;
    procedure SelectAll(aSelect: Boolean = True);
    procedure LoadFromFile(aFileName: string); virtual;
    procedure LoadFromStream(aStream: TStream); virtual;
    procedure LoadScript(aScript: TStrings); virtual;
    property MsgManager: TMsgManager read fMsgMngr;
    property BreakOnError: Boolean read fBreakOnError write fBreakOnError default False;
    property DatabaseType: TDatabaseType read fDatabaseType;
    property ShowFullLog: Boolean read GetShowFullLog;
    property IsUpdateScript: Boolean read fIsUpdate;
    property Statements[ix: Integer]: TStatement read GetStatement write SetStatement;
    property StatementsCount: Integer read GetStatementsCount;
    property OnAfterStatementExecute: TStatementExecuteEvent
             read fOnAfterStatementExecute write fOnAfterStatementExecute;
    property OnBeforeStatementExecute: TStatementExecuteEvent
             read fOnBeforeStatementExecute write fOnBeforeStatementExecute;
    property OnErrorMessage: TErrorMessageEvent
             read fOnErrorMessage write fOnErrorMessage;
    property OnLogNextLine: TLogNextLineEvent
             read fOnLogNextLine write fOnLogNextLine;
    property OnProgressUpdate: TProgressUpdateEvent
             read fOnProgressUpdate write fOnProgressUpdate;
    property OnStatementCreate: TStatementCreateEvent
             read fOnStatementCreate write fOnStatementCreate;
    property OnStatementAdd: TStatementAddEvent
             read fOnStatementAdd write fOnStatementAdd;
    property OnLoadScript: TLoadScriptEvent read fOnLoadScript
                                            write fOnLoadScript;
    property AutoDDL: Boolean read fAutoDDL write fAutoDDL;
    property Errors: Integer read fErrors write fErrors;
    {$ifdef Delphi2009}
    property Encode: TdbcEncode read fEncode write fEncode default encDefault;
    {$endif}
    property CurrentSchema: String read fCurrentSchema write SetCurrentSchema;
    // add statement
    function Add(aText: String;
                 aTyp:TStatType;
                 aObjName: String;// = '';
                 aObjType: TDbObject;// = dboUnknown;
                 aOperation: TDbOperation = odbUnknown;
                 aOwnName: String = '';                  // owner: table / view / procedure (for field, param)
                 aTerm: String = ''): TStatement; virtual;
    // add comment into result script
    function AddComment(Comment: String): TStatement; virtual;
    // add RECONNECT statement
    procedure AddReConnect(); virtual;
    // parse database connect string
    procedure ParseConnect(const Str: String); virtual;
    // clear update script
    procedure Clear(); virtual;
    // delete statement
    procedure Delete(index: Integer); virtual;
    // add CommitWork
    procedure Add_Commit(); virtual;
    // add object description
    procedure AddDescription(dbObj: TDbObj); virtual;
    // Get result script text
    procedure GetScript(sList: TStrings; LineReNumber: Boolean = True); virtual;
    function ExecuteScript(): Boolean; virtual;
    procedure DoMsgErr(aText: String; aObj: TObject = nil);
    procedure DoMsgWrn(aText: String; aObj: TObject = nil);
  end;

  { Base class for all DB-objects }
  TDbObj = class
  private
    fRefObj: TDbObj;
    fCompRules: TList;
    fPumpRules: TList;
    fDescription: String; // object description
  protected
    fObjs: TCustomObjs; // owner (db-storage)
    // current state target-db objects
    fObjCreated: Boolean; // new object (not exists in target-DB) into target-DB created
    // Dependencies
    fUseIn: TStringList;  // This object uses into... /   ...
    fUseOf: TStringList; // This object use of objects... /  ...
{$IFDEF DEBUG}
    aUseIn: Pointer;
    aUseOf: POinter;
{$ENDIF}
    // DBComparer may modyfy internal declaration of differenced db-object by update process
    // fSourceDefinition save object declaration on initial state
    fSourceDefinition: String;
    // current object definition (for updated target object)
    fCurrentDefinition: String;
    fBody: String;
    fObjName: String;
    fSchema: String;
    fImageIndex: Integer;
    fObjType: TDbObject;
    function GetSQLExec(): TCustomSQLExec;
    function GetDatabaseType(): TDatabaseType;
    procedure SetDescription(aDescription: String);
    function GetObjCreated(): Boolean; virtual;
    procedure SetObjCreated(aCreated: Boolean); virtual;
    // object caption for display
    function GetCaption(): String; virtual;
    function GetUpdateDescriptionType: TUpdateDescriptionType;
    function GetCompareObjects(): TCmpObjSetType;
    function GetIBServerOptions(): TIBserverOptions;
    function IBDescriptionDef(): String;
    function MSSQLDescriptionDef(): String;
    function ASADescriptionDef(): String;
    function OracleDescriptionDef(): String;
    function MySQLDescriptionDef(): String;
    function PGSQLDescriptionDef(): String;
    procedure SetBody(aBody: String);
    procedure SetObjName(aName: String);
    procedure SetSchema(aName: string);
    function GetObjName(): String;
    function GetObjFullName(): string;
    procedure SetRefObj(aObj: TDbObj);
    function GetImageIndex(): Integer; virtual;
    function GetObjType(): TDbObject;
    procedure SetObjType(aTyp: TDbObject);
    function GetDbPrefix(): String;
    // get ObjName without db_prefix
    function GetShortObjName(): String;
    function GetRefObjName(): String;
    function GetObjKeyDef(): String; virtual;
  public
    SysName: String; // internal (system) DB-object name
    ObjID: Integer;
    Text: String; // source text
    Line: Integer;
    OwnUser: string; // owner (user)
    SchemaPath: String; // db2 schema path
    MsQuotedIdent: Boolean; // MSSQL: Quoted_Identifier
    MsAnsiNulls: Boolean;   // MSSQL: Ansi_Nulls
    CompExclude: Boolean;   // exclude from comparison
    property Body: String read fBody write SetBody;
    // source object name (in database)
    property ObjName: String read GetObjName write SetObjName;
    // without db_prefix
    property ShortObjName: String read GetShortObjName;
    property RefObjName: String read GetRefObjName;
    property Schema: String read fSchema write SetSchema;
    property ObjFullName: String read GetObjFullName;
    property ImageIndex: Integer read GetImageIndex write fImageIndex;
    property IBserverOptions: TIBServerOptions read GetIBServerOptions;
    property CompareObjects: TCmpObjSetType read GetCompareObjects;
    property SourceDefinition: string read fSourceDefinition write fSourceDefinition;
    property Objs: TCustomObjs read fObjs;
    property UseIn: TStringList read fUseIn;  // This object uses into... /   ...
    property UseOf: TStringList read fUseOf; // This object use of objects... /  ...
    property SQLExec: TCustomSQLExec read GetSQLExec;
    property DatabaseType: TDatabaseType read GetDatabaseType;
    property ObjCreated: Boolean read GetObjCreated write SetObjCreated;
    property Description: String read fDescription write SetDescription;
    property Caption: String read GetCaption;
    property CurrentDefinition: String read fCurrentDefinition write fCurrentDefinition;
    property UpdateDescriptionMode: TUpdateDescriptionType
             read GetUpdateDescriptionType;
    // The reference to the same object in DB-Tree (master <--> target)
    property RefObj: TDbObj read fRefObj write SetRefObj;
    property CompRules: TList read fCompRules;
    property PumpRules: TList read fPumpRules;
    property ObjType: TDbObject read GetObjType write SetObjType;
    property db_prefix: String read GetDbPrefix;
    property ObjKeyDef: String read GetObjKeyDef;
    constructor Create(aObjs: TCustomObjs);
    destructor Destroy; override;
    function Def(): String; virtual; // object DDL-definition
    function CompareDef(): string; virtual;
    Function UseInActive(): Boolean; // This object is used in any other object
    function UseOfActive(): Boolean; // This object uses (causes) any other object
    // Create UseOf... objects before this object creating, and create this...
    procedure CreateDependDef(); virtual;
    // Drop UseIn... objects before this object dropping, and drop this...
    procedure DropDependDef(); virtual;
    // call before object dropping
    procedure DoBeforeDrop(); virtual;
    procedure DoAfterDrop(); virtual;
    // call before object creating
    procedure DoBeforeCreate(); virtual;
    // call after object creating
    procedure DoAfterCreate(); virtual;
    // CREATE... DDL-definition
    function CreateDef(): String; virtual;
    // DROP... DDL-definition
    function DropDef(): String; virtual;
    // ALTER... DDL-definition
    function AlterDef(): String; virtual;
    // for modifing procedures & triggers body - source state
    function SourceDef(): String; virtual;
    // generate comment string from source aComment
    function GenComment(aComment: String): String; virtual;
    // Quote ident if need - for SQL DIALECT 3
    function QuoteIdent(Ident: String): String; virtual;
    function DescriptionDef(): String; virtual;
  end;

  TDependObj = Class(TDbObj)
  public
    ObjField: String; // optional
    Obj: TDbObj;
    Active: Boolean;
    Computed: Boolean;
    tblObj: TDbObj; // table (relation)
    // really view saved into rdb$dependencies as relation
    viwObj: TDbObj; // view
    PosX, PosY: integer; // Pos / Line into script
    constructor Create(aObjs: TCustomObjs);
    destructor Destroy; override;
    function GetCaption(): String; override;
  end;

  { Abstract database extractor }
  TCustomExtract = Class(TComponent)
  protected
    fOnProgressUpdate: TProgressUpdateEvent;
    fOnProgressUpdateEx: TProgressUpdateExEvent;
    fOnLogNextLine: TLogNextLineEvent;
    fOnErrorMessage: TErrorMessageEvent;
    fOnDebugMessage: TLogNextLineEvent;
    fProgressMaxValue: integer;
    fProgressValue: integer;
    fShowFullLog: Boolean;
    fStructure: TCustomDBStructure;
    fDatabaseType: TDatabaseType;
    fExtractObjects: TCmpObjSetType;
    fMsgMngr: TMsgManager;
    fInclObjsNames, fExclObjsNames: TStrings;
    fOnAcceptObj: TAcceptObjEvent;
    fOnAccept_Obj: TAccept_ObjEvent;
    procedure SetOnProgressUpdate(aEvent: TProgressUpdateEvent); virtual;
    procedure SetOnErrorMessage(aEvent: TErrorMessageEvent); virtual;
    procedure SetOnLogNextLine(aEvent: TLogNextLineEvent); virtual;
    procedure SetDBStructure(aDBStructure: TCustomDBStructure); virtual;
    function GetCompareObjects: TCmpObjSetType; virtual;
    procedure DoProgressUpdate(Value, MaxValue: Integer); virtual;
    procedure DoProgressUpdateEx(Detail: Boolean; aText: String; Value, MaxValue: Integer); virtual;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    function GetMsgManager(): TMsgManager; virtual;
    procedure DoDbgMsg(aTxt: String); virtual;
    procedure SetInclObjsNames(aValue: TStrings);
    procedure SetExclObjsNames(aValue: TStrings);
    function LikeObjName(aObjName, Mask: String): Boolean;
    function IsInclObjName(aValue: String): Boolean;
    function IsExclObjName(aValue: String): Boolean;
    function AcceptObj(aObjName: String; aObjType: TDbObject): Boolean;
    function Accept_Obj(aObj: TDbObj): Boolean;
    procedure ObjsAccept_Obj(Sender: TObject; aObj: TDbObj; var Accept: Boolean);
  public
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    procedure DoErrorMessage(ErrMsg: String); virtual;
    procedure AddObj(aObj: TDbObj);
    property DatabaseType: TDatabaseType read fDatabaseType;
    property ShowFullLog: Boolean
             read fShowFullLog write fShowFullLog default True;
    property DBStructure: TCustomDBStructure
             read fStructure write SetDBStructure;
    property ExtractObjects: TCmpObjSetType read fExtractObjects
                                            write fExtractObjects
                                            default DefCmpObjSet;
    property MsgManager: TMsgManager read GetMsgManager;
    property IncludeObjsNames: TStrings read fInclObjsNames write SetInclObjsNames;
    property ExcludeObjsNames: TStrings read fExclObjsNames write SetExclObjsNames;
    property OnProgressUpdate: TProgressUpdateEvent
             read fOnProgressUpdate write SetOnProgressUpdate;
    property OnProgressUpdateEx: TProgressUpdateExEvent
      read fOnProgressUpdateEx write fOnProgressUpdateEx;
    property OnLogNextLine: TLogNextLineEvent
             read fOnLogNextLine write SetOnLogNextLine;
    property OnErrorMessage: TErrorMessageEvent
             read fOnErrorMessage write SetOnErrorMessage;
    property OnDebugMessage: TLogNextLineEvent
             read fOnDebugMessage write fOnDebugMessage;
    property OnAcceptObj: TAcceptObjEvent read fOnAcceptObj write fOnAcceptObj;
    property OnAccept_Obj: TAccept_ObjEvent read fOnAccept_Obj write fOnAccept_Obj;
    function ExtractDatabase(): Boolean; overload; virtual;
  end;

  TCustomDBStructure = Class(TComponent)
  protected
    fObjs: TCustomObjs;
    fExtractor: TCustomExtract;
    fdb_prefix: String;
    procedure SetDatabaseType(aType: TDatabaseType);
    function GetDatabaseType(): TDatabaseType;
    procedure SetDbPrefix(aValue: String);
    function GetDbPrefix(): String;
  public
    property db_prefix: String read fdb_prefix write SetDbPrefix;
    property DatabaseType: TDatabaseType Read GetDatabaseType write SetDatabaseType;
    property Objs: TCustomObjs read fObjs;
    property Extractor: TCustomExtract read fExtractor;
    destructor Destroy; override;
    procedure Clear; virtual;
  end;

  TCustomDBComparer = Class(TComponent)
  protected
    fOnLogNextLine: TLogNextLineEvent;
    fOnProgressUpdate: TProgressUpdateEvent;
    fOnErrorMessage: TErrorMessageEvent;
    fProgressMaxValue: integer;
    fProgressValue: integer;
    fInternalSQLExec: TCustomSQLExec;
    fSQLExec: TCustomSQLExec;      // Update-script statements sequence
    fDatabaseType: TDatabaseType;
    fCompareObjects: TCmpObjSetType;
    fDBMaster: TCustomDBStructure;
    fDBTarget: TCustomDBStructure;
    fShowFullLog: Boolean;
    fWrnScript: TStrings;
    fConnectionOptions: TConnectionOptions;
    fIBServerOptions: TIBComparerOptions;
    fMSSQLServerOptions: TMSSQLServerOptions;
    fMySQLServerOptions: TMySQLServerOptions;
    fASAServerOptions: TASAServerOptions;
    fASEServerOptions: TASEServerOptions;
    fOracleServerOptions: TOracleServerOptions;
    fPGSQLServerOptions: TPGSQLServerOptions;
    fDB2ServerOptions: TDB2ServerOptions;
    fExtractorMaster: TCustomExtract;
    fExtractorTarget: TCustomExtract;
    fAllowExtractMaster: Boolean;
    fAllowExtractTarget: Boolean;
    fAllowCompare: Boolean;
    {$ifdef Delphi2009}
    fEncode: TdbcEncode;
    {$endif}
    function GetMObjs(): TCustomObjs;
    function GetTObjs(): TCustomObjs;
    function GetSQLExec(): TCustomSQLExec;
    procedure SetSQLExec(aSQLExec: TCustomSQLExec);
    procedure SetDBMaster(aDB: TCustomDBStructure);
    procedure SetDBTarget(aDB: TCustomDBStructure);
    procedure LinkDB();
    procedure SetShowFullLog(aLog: Boolean);
    procedure DoProgressUpdate(Value, MaxValue: Integer);
    procedure DoErrorMessage(ErrMsg: String);
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure SetIBServerOptions(aOptions: TIBComparerOptions);
    procedure SetMSSQLServerOptions(aOptions: TMSSQLServerOptions);
    procedure SetMySQLServerOptions(aOptions: TMySQLServerOptions);
    procedure SetASAServerOptions(aOptions: TASAServerOptions);
    procedure SetASEServerOptions(aOptions: TASEServerOptions);
    procedure SetOracleServerOptions(aOptions: TOracleServerOptions);
    procedure SetPGSQLServerOptions(aOptions: TPGSQLServerOptions);
    procedure SetDB2ServerOptions(aOptions: TDB2ServerOptions);
    procedure SetCompareObjects(aCmpObjs: TCmpObjSetType);
  public
    procedure DoLogNextLine(LogLine: String);
    property OnErrorMessage: TErrorMessageEvent
      read fOnErrorMessage write fOnErrorMessage;
    property OnProgressUpdate: TProgressUpdateEvent
      read fOnProgressUpdate write fOnProgressUpdate;
    property OnLogNextLine: TLogNextLineEvent
      read fOnLogNextLine write fOnLogNextLine;
    constructor Create(aOwner: TComponent); override;
    destructor Destroy; override;
    property ShowFullLog: Boolean read fShowFullLog write SetShowFullLog default True;
    // SQL script executive
    property SQLExec: TCustomSQLExec read GetSQLExec write SetSQLExec;
    // master database objects
    property MObjs: TCustomObjs read GetMObjs;
    // target database objects
    property TObjs: TCustomObjs read GetTObjs;
    // warnings
    property WrnScript: TStrings read fWrnScript;
    // Database type: InterBase, SQL Anywhere & etc.
    property DatabaseType: TDatabaseType read fDatabaseType write fDatabaseType;
    property CompareObjects: TCmpObjSetType read fCompareObjects
                                            write SetCompareObjects
                                            default DefCmpObjSet;
    property DBStructureMaster: TCustomDBStructure read fDBMaster write SetDBMaster;
    property DBStructureTarget: TCustomDBStructure read fDBTarget write SetDBTarget;
    property ExtractorMaster: TCustomExtract read fExtractorMaster write fExtractorMaster;
    property ExtractorTarget: TCustomExtract read fExtractorTarget write fExtractorTarget;
    property ConnectionOptions: TConnectionOptions read fConnectionOptions;
    property IBServerOptions: TIBComparerOptions read fIBServerOptions write SetIBserverOptions;
    property MSSQLServerOptions: TMSSQLServerOptions read fMSSQLServerOptions write SetMSSQLServerOptions;
    property MySQLServerOptions: TMySQLServerOptions read fMySQLServerOptions write SetMySQLServerOptions;
    property ASAServerOptions: TASAServerOptions read fASAServerOptions write SetASAServerOptions;
    property ASEServerOptions: TASEServerOptions read fASEServerOptions write SetASEServerOptions;
    property OracleServerOptions: TOracleServerOptions read fOracleServerOptions write SetOracleServerOptions;
    property PGSQLServerOptions: TPGSQLServerOptions read fPGSQLServerOptions write SetPGSQLServerOptions;
    property DB2ServerOptions: TDB2ServerOptions read fDB2ServerOptions write SetDB2ServerOptions;
    property AllowExtractMaster: Boolean read fAllowExtractMaster
                                         write fAllowExtractMaster;
    property AllowExtractTarget: Boolean read fAllowExtractTarget
                                         write fAllowExtractTarget;
    property AllowCompare: Boolean read fAllowCompare
                                         write fAllowCompare;
    {$ifdef Delphi2009}
    property Encode: TdbcEncode read fEncode write fEncode default encDefault;
    {$endif}
    // Quote ident if need - for SQL DIALECT 3
    function QuoteIdent(Ident: String): String;
    function ExtractDatabases(): Boolean; virtual;
    procedure CompareDatabases(); virtual;
    procedure ExtractAndCompareDatabases(); virtual;
  end;

const
  CrLf = #$0D#$0A;

function Quote_Ident(aIdent: String; DatabaseType: TdatabaseType; aServerVersion: TIBSQLServerVersionType; SQLDialect: TIBSQLDialectType = 3): String;
{$IFNDEF DELPHI2005}
function ReplaceStr(const AText, AFromText, AToText: string): string;
{$ENDIF}

implementation

//The full source code is available on purchasing the product

end.
